package com.cx.mall.user.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cx.mall.user.dto.UserReceiveAddressDTO;
import com.cx.mall.user.entity.UserInfo;
import com.cx.mall.user.entity.UserReceiveAddress;
import com.cx.mall.user.mapper.IUserReceiveAddressMapper;
import com.cx.mall.user.service.IUserReceiveAddressService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserReceiveAddressServiceImpl extends ServiceImpl<IUserReceiveAddressMapper, UserReceiveAddress> implements IUserReceiveAddressService {


    @Override
    public List<UserReceiveAddress> listAddressByUserId(Long userId) {
        //  条件构造器
        LambdaQueryChainWrapper<UserReceiveAddress> wrapper = new LambdaQueryChainWrapper<>(this.getBaseMapper());
        //  构建条件，并返回查询结果
        List<UserReceiveAddress> list = wrapper.eq(UserReceiveAddress::getUserId, userId).list();
        return list;
    }

    @Override
    public List<UserReceiveAddressDTO> listByUserId(Long userId) {
        //  条件构造器
        LambdaQueryChainWrapper<UserReceiveAddress> wrapper = new LambdaQueryChainWrapper<>(this.getBaseMapper());
        //  构建条件，并返回查询结果
        List<UserReceiveAddress> list = wrapper.eq(UserReceiveAddress::getUserId, userId).list();
        //  将UserReceiveAddress转换成UserReceiveAddressDTO对象
        List<UserReceiveAddressDTO> collect = list.stream().map(e -> {
            String  json = JSON.toJSONString(e);
            UserReceiveAddressDTO dto = JSON.parseObject(json, UserReceiveAddressDTO.class);
            return dto;
        }).collect(Collectors.toList());
        return collect;
    }
}
